package com.asset.web.auth;

import com.asset.dto.UserDto;
import com.asset.pojo.User;
import com.asset.service.RoleService;
import com.asset.service.UserService;
import com.asset.service.impl.UserServiceImpl;
import com.asset.util.PageResult;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 11725
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    private static final String USER_INFO = "USER_INFO";

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    /**
     * 查询指定用户信息
     * @param id            用户 id
     * @return              UserDto 对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<UserDto> selectById(@PathVariable("id") Long id) {
        UserDto userDto = userService.selectById(id);
        return ResponseEntity.ok(userDto);
    }

    /**
     * 获取登录用户的个人信息
     * @return
     */
    @GetMapping("/verify")
    public ResponseEntity<UserDto> verify() {
        return ResponseEntity.ok(userService.verify());
    }

    /**
     * 分页查询匹配的用户信息
     * @param page          当前页码
     * @param rows          单页记录数
     * @param keyword       关键字：用户名 或 真实姓名
     * @param depaId        部门编号
     * @param rid           角色编号
     * @return              用户对象集合
     */
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/list")
    public ResponseEntity<PageResult<UserDto>> selectByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "depaId", required = false) Long depaId,
            @RequestParam(value = "rid", required = false) Long rid
    ) {
        PageResult<UserDto> result = userService.selectByPage(page, rows, keyword, depaId, rid);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据 id 列表查询用户信息
     * @param ids
     * @return
     */
    @PreAuthorize("hasRole('USER')")
    @GetMapping
    public ResponseEntity<List<UserDto>> selectByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(userService.selectByIds(ids));
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Void> insert(@RequestBody User user) {
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    @PreAuthorize("hasAnyRole('USER', 'ADMIN')")
    public ResponseEntity<Void> update(@RequestBody User user) {
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 为指定用户分配角色
     * @param id            用户编号
     * @param roleList      角色 id 列表
     * @return              204
     */
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateRoles(@PathVariable Long id, @RequestBody List<Long> roleList) {
        roleService.updateUserRoles(id, roleList);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
